﻿package velbus.modules 
{
	import com.kingnare.skins.PopUpButton.PopUpButtonSkin;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.sampler.NewObjectSample;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import mx.controls.Button;
	import mx.events.FlexMouseEvent;
	import velbus.modules.part.Switch;
	import velbus.Packet;
	import velbus.Velbus;
	import velbus.VelbusEvent;
	import velbus.modules.VelbusModuleEvent;
	
	/**
	* ...
	* @author Gert Stalpaert
	*/
	public class PushButtonAndTimer extends VelbusModule
	{
		
		public static const MODULE_TYPE:int = 0x0B;
		public static const COMMAND_WRITE_DATA_TO_MEMORY:int = 0xFC;
		
		private var _nameListCounter:int = 0;
				
		public function PushButtonAndTimer(velbus:Velbus, address:int) 
		{
			super(velbus, address);
			this.removeChild(_taInfo);
			title = "Push Button And Timer Panel " + Velbus.dec2hex(_address)
			addEventListener(VelbusModuleEvent.NAME_LIST_CHANGE, onNameListChange);
			
		}
		
		private function onNameListChange(e:VelbusModuleEvent):void 
		{
			//delete previous
			this.removeAllChildren();
			_nameListCounter++
			if (_nameListCounter == NrOfNames * 3) { //only add switches if all name data is read
				for (var i:int = 0; i < _nameList.length; i++) 
				{
					var sw:Switch = new Switch(_velbus, _address, i, _nameList[i]);
					this.addChild(sw);
				}
				
				///request status to fill the switch statusses
				var ba:ByteArray = new ByteArray();
				ba[0] = VelbusModule.REQUEST_MODULE_STATUS;
				ba[1] = 0xFF // ask for all 8 buttons
				Velbus.addToSendQueue(new Packet(_address, Packet.PRIORITY_LOW, 2, ba, false));
			}
		}

		
		
		override protected function get NrOfNames():int
		{
			return 8;
		}
		
		/**
		 * 
		 * @param	address
		 * @param	lineNr	1 to 4 (3 and 4 are for the second screen)
		 * @param	label	max 16 chars
		 * @param	restoreAfter	(under construction) restore the lcd line back to its previous value after x ms. null = no restore
		 */
		static public function setLcdLine(address:int, lineNr:int, label:String, restoreAfter:Number = NaN): void 
		{
			VelbusSocket.info.text += "PushButtonAndTimer.setLcdLine > address : " + address + ", lineNr : " + lineNr + ", label : " + label  + "\n";
			if (address < 0 || address > 255) {
				VelbusSocket.info.text += "Error: setLcdLine: address should be between 0 and 255"  + "\n";
				return;
			}
			if (lineNr < 1 || lineNr > 4) {
				VelbusSocket.info.text += "Error: setLcdLine: line Nr should be between 1 and 4"  + "\n";
				return
			}

			label = noSpecialCharacters(label);
			
			var pos:int = 0x80 + ((lineNr - 1) * 16);
			var char:int;
			for (var i:int = 0; i < 16; i++) 
			{
				if (label.length < i + 1) {
					label += " ";
				}
				char = label.charCodeAt(i);
				
				var ba:ByteArray = VelbusModule.ArrayToByteArray([PushButtonAndTimer.COMMAND_WRITE_DATA_TO_MEMORY, 0x00, pos, char]);
				var p:Packet = new Packet(address, Packet.PRIORITY_LOW, 4, ba, false);
				Velbus.addToSendQueue(p);	
				pos++;
			}
			/*
			if (restoreAfter) {
				var t:Timer = new Timer(restoreAfter, 1);
				t.addEventListener(TimerEvent.TIMER, onRestoreLine);
				t.start();
			}
			*/
		}
		
		static public function noSpecialCharacters(s:String):String
		{
			s = s.split("á").join("a");
			s = s.split("à").join("a");
			s = s.split("â").join("a");
			s = s.split("ä").join("a");
			s = s.split("é").join("e");
			s = s.split("è").join("e");
			s = s.split("ê").join("e");
			s = s.split("ë").join("e");
			s = s.split("ó").join("o");
			s = s.split("ò").join("o");
			s = s.split("ô").join("o");
			s = s.split("ö").join("o");
			s = s.split("í").join("i");
			s = s.split("ì").join("i");
			s = s.split("î").join("i");
			s = s.split("ï").join("i");
			s = s.split("ú").join("u");
			s = s.split("ù").join("u");
			s = s.split("û").join("u");
			s = s.split("ü").join("u");
			s = s.split("ç").join("c");
			return s;
		}
		
		/*
		static private function onRestoreLine(e:TimerEvent):void 
		{
			var t:Timer = e.target as Timer;
			t.stop();
			
		}
		*/
	}
	
}